!  Copyright:  1991-1997 Global Village Communication, Inc.     All Rights Reserved.
! 
!	Portions Authored by: Kris Kreutzman
!	Portions Copyright:	 1991-1996 Apple Computer, Inc.	All Rights Reserved.
!
!	revision history:
!	1.0.9 06/25/97 GAS		Fixed ARAP Callback issue by adding E1 on modem reset
!   1.0.8 06/11/97 GAS		Fixed hang up code and added TIES support
!   1.0.7 04/25/97 GAS		Fixed 19,200 call-back limitation
!   1.0.6 04/16/97 GAS		Modified to support X2 speeds, added new X2 config string
!                           and commented out Fax/ARA discrimination code
!   1.0.5 03/12/97 GAS		Updated code to support K56Flex speeds
!	1.0.4 01/31/97 RHY		Removed the \^ from diailng strings to turn off data
!							calling tones.
! 	1.0.3 11/15/96 DEJ 		Added fix for problem when "Ring through modem" is on and 
!							making ARA connection.
!   1.0.1 09/05/96 DEJ - 	Generated basic Apple/Kris Kreutzman script w/MSG 1.0a1.
!		                    Integrated earlier/cleaner answer & hangup (rhoiberg),
!                           Platinum mods (SAP), Fax/ARA discrimination (crw), Int'l 
!                           Blacklisting & blind dialing (DEJ), 31.2k & 33.6k (gas).
! 
!  'mlts' resource info for this modem:
!    byte 1 == 01 -> modem HAS built-in error correction protocols
!    byte 2 == 01 -> modem HAS built-in data compression protocols
!    byte 3 == 40 -> max number of chars (hex 28) in varstr 7
!    byte 4 == 40 -> max number of chars (hex 28) in varstr 8
!    byte 5 == 40 -> max number of chars (hex 28) in varstr 9
!	
@ORIGINATE
@ANSWER
! 
! ---- Initial modem setup ----
! 
! In originate mode, set serial port speed depending upon the compression flag
!	A higher rate with compression on (PPP) to handle expanded data from the modem
!	A lower rate closer to the DCE when compression is off (ARA)
! 
ifstr 5 1 "0"
serreset 115200, 0, 8, 1
jump 2
!
@LABEL 1
serreset 57600, 0, 8, 1
!
@LABEL 2
hsreset 0 0 0 0 0 0
settries 0
!
! Get the modem's attention
!
matchclr
matchstr 1 3 "OK\13\10"
write "AT\13"
matchread 60
pause 30
!
@LABEL 3
! 
! Setup the modem for the following:
!   AT         Attention
!   &F1        Reset to factory settings
!	&D0		   DTR override
!	&H1		   Enable CTS Hardware flow control
!	&R1		   Disable RTS flow control to prevent RTS/DTR Toggle hangup
!	&B1		   Disable autobaud/lock DTE
!	S0=0	   Turn off auto-answer
!	E0V1Q0	   Turn command echo off and enable verbose responses
!	&A3		   Protocol indicators added (LAPM/MNP/NONE)
!   S7=60      Allow 60 seconds for international calls to complete
!
matchclr
matchstr 1 4   "OK\13\10"
matchstr 2 101 "ERROR\13\10"
write "AT&F1&D0&H1&R1&B1S0=0E0V1Q0&A3S7=60\13"
matchread 90
inctries
iftries 3 101
!
! Reset the Modem on setup failure
!
DTRClear
pause 5
DTRSet
flush
pause 90
jump 3
!
!
@LABEL 4
! 
! Modem now responding and configured for full EC/DC functions.
! 
! Varstring 4 , reliable link protocol:
!    = 0, handled by computer (ARAP)
!    = 1, handled by modem (PPP)
!    = 2, MNP10 protocol (Cellular protocol, no longer supported)
! 
! If PPP or Cellular mode, jump to 5, otherwise (ARA) disable EC/DC.
! 
ifstr 4 5 "1"
ifstr 4 5 "2"
!
! Varstring 4 == 0, turn off reliable link protocol in modem (ARAP)
! 
matchclr
matchstr 1 9 "OK\13\10"
write "AT&M0\13"
matchread 30
jump 101
!
!
@LABEL 5
! 
! This is where MNP10 (or MNP10EC) could go, if/when we support that.
! 
! Standard EC/DC is still on.  Decide whether to turn off DC.
! Varstring 5, compression protocol:
!    = 0, handled by computer 
!    = 1, handled by modem
! 
ifstr 5 9 "1"
!
! Varstring 5 == 0, turn off compression protocol in modem.
matchclr
matchstr 1 9 "OK\13\10"
write "AT&K0\13"
matchread 30
jump 101
!
!
@LABEL 9
! 
! If speaker on flag is true, jump to 13.  Otherwise, turn the speaker off.
! 
! Varstring 2, modem speaker:
!    = 0, speaker off
!    = 1, speaker on
! 
ifstr 2 13 "1"
pause 5
matchclr
matchstr 1 13 "OK\13\10"
write "ATM0\13"
matchread 30
jump 101
!
! Modem ready, wait for a call or originate a call
!
@LABEL 13 
ifANSWER 32
!
!
! ---- Originating a call ----
! 
! For normal dialing, jump to 19.  Otherwise, manual dial jumps to 15, blind dial
!    jumps to 17.
! 
! Varstring 6, dialing mode:
!    = 0, normal dialing
!    = 1, blind dialing
!    = 2, manual dialing
ifstr 6 17 "1"
ifstr 6 15 "2"
jump 19
!
@LABEL 15
! 
! Display ASK dialog with message.  Goto label 107 if dialog canceled.
! 
ASK 2 "Pick up the phone & dial ^1.  Hit OK when the phone rings, then hangup." 107
note "Manual dialing initiated" 3
! 
! X1S6=4 to ignore dialtone 4 seconds, & ignore busy, D to dial, \^ generates data tone
write "ATX1D\^\13"
jump 32
!
@LABEL 17
note "Dialing without tone" 3
matchclr
matchstr 1 19 "OK\13\10"
! X1 to ignore dialtone 4 seconds, & ignore busy for blind dialing.
write "ATX1\13"
matchread 30
jump 101
!
!
@LABEL 19
! 
! This is where we break up long dialstrings
! 
! Display the full dialstring contained in Varstring 1
note "Dialing ^1" 3
!
! Varstrings 7, 8 and 9, contain dialstring fragments
!    Long phone numbers may have been split into smaller groups
!    so that the modem can use them
! Check to see whether these fragments exist.  If they do, dial them one at a time.
! If not, jump ahead and dial the remaining phone number.  Use the data tone.
! 
! Varstring 3:  "p" for pulse & "t" for tone dialing
! Varstring 8 == blank (dialstring in varstring 7)
! Varstring 9 == blank (dialstring in varstrings 7 & 8)
! Otherwise (dialstring in varstrings 7, 8 & 9)
ifstr 8 27 " "
ifstr 9 24 " "
!
!  Write dialstring in varstrings 7, 8 & 9
matchclr
matchstr 1 21 "OK\13\10"
write "ATD^3^7;\13"
matchread 400
jump 101
! 
@LABEL 21
matchclr
matchstr 1 22 "OK\13\10"
write "ATD^3^8;\13"
matchread 400
jump 101
! 
@LABEL 22
write "ATD^3^9\13"
jump 32
!
!
@LABEL 24
!  Write dialstring in varstrings 7 & 8
matchclr
matchstr 1 25 "OK\13\10"
write "ATD^3^7;\13"
matchread 400
jump 101
! 
@LABEL 25
write "ATD^3^8\13"
jump 32
!
@LABEL 27
!  Write dialstring in varstring 7
write "ATD^3^7\13"
!
!
!    ---- Connection response ----
!
! The following section will parse modem responses of four types:
!   1) PROTOCOL: xxx, COMPRESSION: xxx, CONNECT xxx
!   2) CONNECT xxx/ARQ/V42
!   3) Standard incoming and outgoing call progress messages
!   5) International DELAYED & BLACKLISTED restrictions.
!
@LABEL 32
matchclr
matchstr  1 81  "RING\13\10"
matchstr  2 102 "NO DIAL TONE\13\10"
matchstr  3 103 "NO CARRIER"
matchstr  4 103 "ERROR\13\10"
matchstr  5 104 "BUSY\13\10"
matchstr  6 105 "NO ANSWER\13\10"
matchstr  7 33  "CONNECT "
matchstr  8 32  "CARRIER"
matchstr  9 40  "CONNECT\13\10"
matchstr 10 74  "PROTOCOL: LAP"
matchstr 11 74  "PROTOCOL: MNP"
matchstr 12 74  "PROTOCOL: ALT"
matchstr 13 76  "COMPRESSION: V"
matchstr 14 76  "COMPRESSION: MNP5"
matchstr 15 76  "COMPRESSION: CLASS"
matchstr 16 110  "DELAYED  "
matchstr 17 111  "BLACKLISTED\13\10"
matchread 700
ifANSWER 32
jump 101
!
!  Parse the speed of connect result codes
!  4800 has two entries in order
!  to distinguish from 48000
! 
!
@LABEL 33
matchclr
matchstr  1 41 "4800\13"
matchstr  2 41 "4800/"
matchstr  3 42 "7200"
matchstr  4 43 "9600"
matchstr  5 44 "12000"
matchstr  6 45 "14400"
matchstr  7 46 "16800"
matchstr  8 47 "19200"
matchstr  9 48 "21600"
matchstr 10 49 "24000"
matchstr 11 50 "26400"
matchstr 12 51 "28800"
matchstr 13 52 "31200"
matchstr 14 53 "32000"
matchstr 15 54 "33600"
matchstr 16 55 "33333"
matchstr 17 56 "36000"
matchstr 18 57 "37333"
matchstr 19 58 "40000"
matchstr 20 59 "41333"
matchstr 21 60 "42666"
matchstr 22 61 "44000"
matchstr 23 62 "45333"
matchstr 24 63 "46666"
matchstr 25 64 "48000"
matchstr 26 65 "49333"
matchstr 27 66 "50666"
matchstr 28 67 "52000"
matchstr 29 68 "53333"
matchstr 30 69 "54666"
matchstr 31 70 "56000"
matchread 30
jump 71
!
! -- Connection rates --
! CommunicatingAt informs ARA of the raw modem to modem
! connection speed.
!
!
@LABEL 39
note "Communicating at 1200 bps." 2
CommunicatingAt 1200
jump 72
!
@LABEL 40
note "Communicating at 2400 bps." 2
CommunicatingAt 2400
jump 72
!
@LABEL 41
note "Communicating at 4800 bps." 2
CommunicatingAt 4800
jump 72
!
@LABEL 42
note "Communicating at 7200 bps." 2
CommunicatingAt 7200
jump 72
!
@LABEL 43
note "Communicating at 9600 bps." 2
CommunicatingAt 9600
jump 72
!
@LABEL 44
note "Communicating at 12000 bps." 2
CommunicatingAt 12000
jump 72
!
@LABEL 45
note "Communicating at 14400 bps." 2
CommunicatingAt 14400
jump 72
!
@LABEL 46
note "Communicating at 16800 bps." 2
CommunicatingAt 16800
jump 72
!
@LABEL 47
note "Communicating at 19200 bps." 2
CommunicatingAt 19200
jump 72
!
@LABEL 48
note "Communicating at 21600 bps." 2
CommunicatingAt 21600
jump 72
!
@LABEL 49
note "Communicating at 24000 bps." 2
CommunicatingAt 24000
jump 72
!
@LABEL 50
note "Communicating at 26400 bps." 2
CommunicatingAt 26400
jump 72
!
@LABEL 51
note "Communicating at 28800 bps." 2
CommunicatingAt 28800
jump 72
!
@LABEL 52
note "Communicating at 31200 bps." 2
CommunicatingAt 31200
jump 72
!
@LABEL 53
note "Communicating at 32000 bps." 2
CommunicatingAt 32000
jump 72
!
@LABEL 54
note "Communicating at 33600 bps." 2
CommunicatingAt 33600
jump 72
!
@LABEL 55
note "Communicating at 33333 bps." 2
CommunicatingAt 33333
jump 72
!
@LABEL 56
note "Communicating at 36000 bps." 2
CommunicatingAt 36000
jump 72
!
@LABEL 57
note "Communicating at 37333 bps." 2
CommunicatingAt 37333
jump 72
!
@LABEL 58
note "Communicating at 40000 bps." 2
CommunicatingAt 40000
jump 72
!
@LABEL 59
note "Communicating at 41333 bps." 2
CommunicatingAt 41333
jump 72
!
@LABEL 60
note "Communicating at 42666 bps." 2
CommunicatingAt 42666
jump 72
!
@LABEL 61
note "Communicating at 44000 bps." 2
CommunicatingAt 44000
jump 72
!
@LABEL 62
note "Communicating at 45333 bps." 2
CommunicatingAt 45333
jump 72
!
@LABEL 63
note "Communicating at 46666 bps." 2
CommunicatingAt 46666
jump 72
!
@LABEL 64
note "Communicating at 48000 bps." 2
CommunicatingAt 48000
jump 72
!
@LABEL 65
note "Communicating at 49333 bps." 2
CommunicatingAt 49333
jump 72
!
@LABEL 66
note "Communicating at 50666 bps." 2
CommunicatingAt 50666
jump 72
!
@LABEL 67
note "Communicating at 52000 bps." 2
CommunicatingAt 52000
jump 72
!
@LABEL 68
note "Communicating at 53333 bps." 2
CommunicatingAt 53333
jump 72
!
@LABEL 69
note "Communicating at 54666 bps." 2
CommunicatingAt 54666
jump 72
!
@LABEL 70
note "Communicating at 56000 bps." 2
CommunicatingAt 56000
jump 72
!
!
@LABEL 71
note "Communicating at an unknown rate." 2
jump 72
!
! Look for reliablilty and compression results 
! at the end of the connect result.
!
@LABEL 72
matchclr
matchstr  1 75 "LAPM"
matchstr  2 75 "REL"
matchstr  3 75 "ARQ"
matchstr  4 77 "COMP/"
matchstr  5 77 "COMP\13"
matchstr  6 75 "V42/"
matchstr  7 75 "V42\13"
matchstr  8 77 "V42BIS"
matchstr  9 77 "V42bis"
matchstr 10 75 "MNP\13"
matchstr 11 77 "MNP5"
matchstr 12 78 "\10"
matchread 30
jump 78
! 
! -- Modem error correction link negotiation --
! Userhook 2 informs ARA that a modem-to-modem error
! correcting protocol has been negotiated
!
!
@LABEL 74
note "Modem Reliable Link Established." 2
userhook 2
jump 32
!
@LABEL 75
note "Modem Reliable Link Established." 2
userhook 2
jump 72
!
! -- Compression negotiation --
! Userhook 3 informs ARA that a modem-to-modem compression
! protocol has been negotiated
!
@LABEL 76
note "Modem Compression Established." 2
userhook 3
jump 32
!
@LABEL 77
note "Modem Compression Established." 2
userhook 3
jump 72
!
!
! -- Normal exit after "CONNECT" --
!
!  This modem has been setup to do CTS handshaking,
!  and we assume that a CTS handshaking cable is being used.
!
@LABEL 78
! Turn on CTS handshaking.
HSReset 0 1 0 0 0 0
!
ifANSWER 79
pause 30
@LABEL 79
exit 0
!
!
! ---- Answer calls ----
!
! A RING result from the modem and in ANSWERING mode
! claims the serial port and aswering the phone
!
@LABEL 81
ifORIGINATE 32
userhook 1
note "Answering phone..." 2
write "ATA\13"
jump 32
! 
! 
! ---- Hang up and reset modem ----
!
@HANGUP 
@LABEL 90 
settries 0
HSReset 0 0 0 0 0 0
!
@LABEL 91
!  Escape from data to command mode using TIES +++AT\13 command
!
pause 60
write "+++"
pause 30 
matchclr
matchstr 1 96 "OK\13\10"
pause 15
write "AT\13"
matchread 60
!
@LABEL 94
! Force a hangup
matchclr
matchstr 1 98 "NO CARRIER\13\10"
matchstr 2 98 "OK\13\10"
matchstr 3 98 "ERROR\13\10"
matchstr 4 98 "0\13\10"
matchstr 5 98 "DELAYED"
matchstr 6 98 "BLACKLISTED"
write "ATH\13"
matchread 30
! 
! Try again to get control of the modem by toggling DTR
!
DTRClear
Pause 5
DTRSet
flush
!
!
! Try the hangup sequence three times otherwise declare an error 
inctries
pause 120
iftries 3 101
jump 91
!
@LABEL 96
! Pause between data and command mode
pause 50
jump 94
!
!
@LABEL 98 
pause 15
matchclr
matchstr 1 99 "OK\13\10"
write "AT&F1E1\13"
matchread 30
jump 101
!
@LABEL 99
exit 0
! 
!
! ---- Error messages -----
!
! Modem Not Responding
@LABEL 101
exit -6019
!
! No Dial Tone
@LABEL 102
exit -6020
!
! No Carrier or Error
@LABEL 103
exit -6021
!
! Busy
@LABEL 104
exit -6022
!
! No Answer
@LABEL 105
exit -6023
!
! User Cancellation
@LABEL 107
exit -6008
!
! 
! ---- DELAYED and BLACKLISTED functions ----
! 
@LABEL 110
matchclr
matchstr  1 119 "00:00:0"
matchstr  2 118 "00:00:1"
matchstr  3 117 "00:00:2"
matchstr  4 116 "00:00:3"
matchstr  5 115 "00:00:4"
matchstr  6 114 "00:00:5"
matchstr  7 113 "00:01:"
matchstr  8 112 "00:02:"
matchread 30
! 
!  The number is 'DELAYED' more than 3 minutes.  Exit the script telling the user to wait or reset his modem.
!
exit -6019 "\13The modem responds 'DELAYED':\13(Cannot redial this number right now.)\13To try again immediately, turn modem OFF and ON before proceeding."
! 
@LABEL 111
! 
!  The number is 'BLACKLISTED'.  Exit the script, telling the user to reset his modem.
! 
exit -6019 "\13The modem responds 'BLACKLISTED':\13(Cannot redial this number again.)\13Double-check the number, then turn your modem OFF and ON before proceeding."
!
@LABEL 112
note "'DELAYED'. Redialing in 3 minutes..."
pause 600
@LABEL 113
note "'DELAYED'. Redialing in 2 minutes..."
pause 600
@LABEL 114
note "'DELAYED'. Redialing in 1 minute... "
pause 100
@LABEL 115
note "'DELAYED'. Redialing in 50 seconds..."
pause 100
@LABEL 116
note "'DELAYED'. Redialing in 40 seconds..."
pause 100
@LABEL 117
note "'DELAYED'. Redialing in 30 seconds..."
pause 100
@LABEL 118
note "'DELAYED'. Redialing in 20 seconds..."
pause 100
@LABEL 119
note "'DELAYED'. Redialing in 10 seconds..."
pause 100
jump 1
! 
! 
! Labels 121-128 are reserved for emergency hacks.
! 
